package edu.columbia.cloudbox;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import edu.columbia.cloudbox.io.LocalIO;
import edu.columbia.cloudbox.policy.CBDBHelper;
import edu.columbia.cloudbox.ui.AllFilesActivity;
import edu.columbia.cloudbox.ui.CachedFilesActivity;
import edu.columbia.cloudbox.ui.ConfigActivity;
import edu.columbia.cloudbox.ui.SavedFilesActivity;

public class CloudBoxActivity extends TabActivity {

	private static final String LOG_TAG = "CloudBoxActivity";
	
	private TabHost tabHost;
	
	private CBDBHelper dbHelper;

	public static TextView pathTextView;
	public static ImageButton backButton;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (!UserStatusService.STARTED) {
			Log.i(LOG_TAG, "Service is not started. Start service!");
			Intent serviceIntent = new Intent(this, UserStatusService.class);
			startService(serviceIntent);
			UserStatusService.STARTED = true;
		}
		while (!UserStatusService.STARTED) {
			Log.i(LOG_TAG, "Starting service...");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		/** Launch database **/
		dbHelper = new CBDBHelper(this);

		if (UserStatusService.USER_LOGGED_IN) {
			Log.i(LOG_TAG, "User: " + UserStatusService.USER_NAME
					+ " has logged in!");

			setContentView(R.layout.main);

			pathTextView = (TextView) findViewById(R.id.title_path);
			backButton = (ImageButton) findViewById(R.id.button_back);

			Resources res = getResources();
			tabHost = getTabHost();

			TabHost.TabSpec spec; // Reusable TabSpec for each tab
			Intent intent; // Reusable Intent for each tab

			tabHost.removeAllViews();
			TabWidget tabs = new TabWidget(this);
			tabs.setId(android.R.id.tabs);
			RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
					android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
			params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
			tabs.setLayoutParams(params);
			FrameLayout content = new FrameLayout(this);
			content.setId(android.R.id.tabcontent);
			content.setLayoutParams(new LinearLayout.LayoutParams(
					android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.FILL_PARENT));
			RelativeLayout relative = new RelativeLayout(this);
			relative.setLayoutParams(new RelativeLayout.LayoutParams(
					android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.FILL_PARENT));
			relative.addView(content);
			relative.addView(tabs);
			tabHost.addView(relative);
			tabHost.setup();

			intent = new Intent().setClass(this, AllFilesActivity.class);
			spec = tabHost
					.newTabSpec("all_files")
					.setIndicator("All Files",
							res.getDrawable(R.drawable.tab_allfiles))
					.setContent(intent);
			tabHost.addTab(spec);
			intent = new Intent().setClass(this, SavedFilesActivity.class);
			spec = tabHost
					.newTabSpec("saved_files")
					.setIndicator("Saved Files",
							res.getDrawable(R.drawable.tab_savedfiles))
					.setContent(intent);
			tabHost.addTab(spec);
			intent = new Intent().setClass(this, CachedFilesActivity.class);
			spec = tabHost
					.newTabSpec("cached_files")
					.setIndicator("Cached Files",
							res.getDrawable(R.drawable.tab_cachedfiles))
					.setContent(intent);
			tabHost.addTab(spec);

			intent = new Intent().setClass(this, ConfigActivity.class);
			spec = tabHost
					.newTabSpec("config")
					.setIndicator("Config",
							res.getDrawable(R.drawable.tab_config))
					.setContent(intent);
			tabHost.addTab(spec);
			tabHost.setCurrentTab(0);

			LocalIO localio = UserStatusService.LocalIO_INSTANCE;
			localio.getPathsByFolder("dummy");

		} else {
			Log.i(LOG_TAG,
					"No user logged in! Redirecting to login activity...");
			Intent intent = new Intent().setClass(this,
					edu.columbia.cloudbox.ui.LoginActivity.class);
			startActivity(intent);
		}
	}
}
